package org.custom.spring.boot.security.service.impl;

import org.custom.spring.boot.security.service.SecurityFilterChainManager;
import org.springframework.stereotype.Service;
import org.springframework.util.PathMatcher;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * Spring-Security拦截器链处理服务
 *
 * @author LiKun
 * @date 2021/11/12 10:59
 */
@Validated
@Service("securityFilterChainManager")
public class SecurityFilterChainManagerImpl implements SecurityFilterChainManager {
    @Resource
    private PathMatcher pathMatcher;

    /**
     * 获取拦截器忽略的请求路径
     *
     * @return 路径
     */
    public static String[] getIgnorePaths() {
        return new String[]{"/css/**", "/js/**", "/images/**", "/approve/login",
                "/favicon.ico", "/doc.html", "/v2/**", "/webjars/**", "/swagger-resources/**"};
    }

    /*----------------------------------------------public static methods---------------------------------------------*/

    @Override
    public boolean whetherTheInterceptorChainHandlesTheRequest(String path) {
        return Arrays.stream(getIgnorePaths()).anyMatch(pattern -> pathMatcher.match(pattern, path));
    }
}
